package org.dyk.mallmanagementsystem.service.user;

import org.dyk.mallmanagementsystem.model.Orders;
import org.dyk.mallmanagementsystem.model.User;
import org.dyk.mallmanagementsystem.repository.OrdersRepository;
import org.dyk.mallmanagementsystem.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServicImpl implements UserService{
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private OrdersRepository ordersRepository;

    /**
     * 查询所有用户 分页
     * @param pageable
     * @return
     */
    @Override
    public Page<User> findAll(Pageable pageable) {
        return userRepository.findAll(pageable);
    }

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    /**
     * 根据 主键 获取用户信息
     * @param uid
     * @return
     */
    @Override
    public User findByUid(int uid) {
        return userRepository.findByUid(uid);
    }

    /**
     * 添加用户
     * @param userLoginName
     * @param userPasswor
     * @param userName
     * @return
     */
    @Override
    public boolean inser(String userLoginName, String userPasswor, String userName) {
        //判断是否有重复登录名
        User byUserLoginName = userRepository.findByUserLoginName(userLoginName);
        if (byUserLoginName == null){
            //验证通过，添加用户
            User user = new User();
            user.setUserLoginName(userLoginName);
            user.setUserPassword(userPasswor);
            user.setUserName(userName);
            userRepository.save(user);
            return true;
        }else {
            //验证未通过
            return false;
        }
    }

    /**
     * 修改用户
     * @param uid
     * @param userLoginName
     * @param userPasswor
     * @param userName
     * @return
     */
    @Override
    public boolean updata(int uid, String userLoginName, String userPasswor, String userName) {
        try{
            //没有重复登录名
            User byUid = userRepository.findByUid(uid);
            byUid.setUid(uid);
            byUid.setUserLoginName(userLoginName);
            byUid.setUserPassword(userPasswor);
            byUid.setUserName(userName);
            userRepository.save(byUid);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @Override
    public boolean delete(int uid) {
        //判断要删除用户是否还有未完成订单
        List<Orders> byUid = ordersRepository.findByUid(uid);
        if (byUid.isEmpty()){
            User user = userRepository.findByUid(uid);
            userRepository.delete(user);
            return true;
        }else {
            return false;
        }
    }

    /**
     * 自定义查询用户
     * @param loginName
     * @param userName
     * @return
     */
    @Override
    public List<User> selectUser(String loginName, String userName) {
        return userRepository.selectUser(loginName, userName);
    }
}
